package lc;
import java.util.*;
import org.junit.*;


public class Ex405 {

    public String toHex(int num) {
        int x = num;
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < 8; i++) {
            int res = 0, d = 0;
            for (int j = 0; j < 4; j++) {
                if ((1 & x) != 0) {
                    res += (int)(Math.pow(2, d));
                }
                d++;
                x >>>= 1;
            }
            sb.insert(0, numToChar(res));
            if (x == 0) break;
        }
        return sb.toString();
    }

    public char numToChar(int n) {
        return n < 10 ? (char)(n + '0') : (char)(n + 'a' - 10);
    }

    @Test
    public void test() {
        System.out.println(toHex(-1));
    }
}